package comic

import (
	"common"
	"common/auth"
	"common/grpcx"
	"common/json_response"
	"common/zapx"
	"gateway/internal/logic"
	"gateway/internal/pb"
	"gateway/internal/types/v1/comic"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type ComicController struct {
	ComicLogic *logic.ComicLogic
}

// NewComicController 新建漫画控制器
func NewComicController() *ComicController {
	controller := &ComicController{
		ComicLogic: logic.NewComicLogic(),
	}
	return controller
}

func (cc *ComicController) ComicList(ctx *gin.Context) {
	var req comic.ComicListRequest
	response := json_response.NewJsonHttpResponse(ctx)
	if err := ctx.ShouldBindQuery(&req); err != nil {
		response.SetCode(json_response.CODE_ERROR).SetMsg(err.Error()).Send()
		return
	}
	response.SetCode(json_response.CODE_SUCCESS).SetData(cc.ComicLogic.GetComicList(req.Title, req.NavigateId, req.Page.Page, req.Page.PerPage)).Send()
	return
}

func (cc *ComicController) ChapterList(ctx *gin.Context) {
	response := json_response.NewJsonHttpResponse(ctx)
	comicId, err := strconv.Atoi(ctx.Param("comic_id"))
	if err != nil {
		zapx.Logger.Error("[ChapterList]解析参数失败", zap.Error(err))
		response.SetCode(json_response.CODE_ERROR).SetMsg("系统错误").Send()
		return
	}
	var req comic.ChapterListRequest
	if err := ctx.ShouldBindQuery(&req); err != nil {
		response.SetCode(json_response.CODE_ERROR).SetMsg(err.Error()).Send()
		return
	}
	response.SetCode(json_response.CODE_SUCCESS).SetData(cc.ComicLogic.GetChapterByComicId(int32(comicId), req.Page.Page, req.Page.PerPage)).Send()
	return
}

func (cc *ComicController) ResourceList(ctx *gin.Context) {
	response := json_response.NewJsonHttpResponse(ctx)
	chapterId, err := strconv.Atoi(ctx.Param("chapter_id"))
	if err != nil {
		response.SetMsg("获取章节id失败").SetCode(json_response.CODE_ERROR).Send()
		return
	}
	images, err := cc.ComicLogic.GetResourceByChapterId(ctx, int32(chapterId))
	if err != nil {
		response.SetMsg(err.Error()).SetCode(json_response.CODE_ERROR).Send()
		return
	}
	response.SetCode(json_response.CODE_SUCCESS).SetData(images).Send()
	return
}

func (cc *ComicController) GetComicDetail(ctx *gin.Context) {
	response := json_response.NewJsonHttpResponse(ctx)
	comicId, err := strconv.Atoi(ctx.Param("comic_id"))
	if err != nil {
		response.SetCode(json_response.CODE_ERROR).SetMsg("参数错误").Send()
		return
	}
	detail, err := cc.ComicLogic.GetComicDetail(ctx, int64(comicId))
	if err != nil {
		response.SetCode(json_response.CODE_ERROR).SetMsg(err.Error()).Send()
		return
	}
	response.SetCode(json_response.CODE_SUCCESS).SetData(detail).Send()
	return
}

func (cc *ComicController) GetHotSearch(ctx *gin.Context) {
	response := json_response.NewJsonHttpResponse(ctx)
	conn, err := grpcx.NewNoCertConn("comic")
	if err != nil {
		zapx.Logger.Error("连接 comic 服务失败", zap.Error(err))
	}
	defer conn.Close()
	client := pb.NewComicClient(conn)
	c, cance := common.WithTimeoutContext(time.Second * 2)
	defer cance()
	res, err := client.GetHotSearchList(c, &pb.GetHotSearchListRequest{
		Limit: 5,
	})
	if err != nil {
		zapx.Logger.Error("调用[GetHotSearchList]失败", zap.Error(err))
	}
	response.SetCode(json_response.CODE_SUCCESS).SetData(res.Name).Send()
	return
}

func (cc *ComicController) ChapterDetail(ctx *gin.Context) {
	response := json_response.NewJsonHttpResponse(ctx)
	chapterId, err := strconv.Atoi(ctx.Param("chapter_id"))
	if err != nil {
		response.SetCode(json_response.CODE_ERROR).SetMsg("参数错误").Send()
		return
	}
	detail, err := cc.ComicLogic.GetChapterDetail(ctx, int32(chapterId))
	if err != nil {
		response.SetCode(json_response.CODE_ERROR).SetMsg(err.Error()).Send()
		return
	}
	response.SetCode(json_response.CODE_SUCCESS).SetData(detail).Send()
	return
}

func (cc *ComicController) ComicLookFor(ctx *gin.Context) {
	response := json_response.NewJsonHttpResponse(ctx)
	var request comic.ComicLookForRequest
	if err := ctx.ShouldBind(&request); err != nil {
		response.SetCode(json_response.CODE_ERROR).SetMsg(err.Error()).Send()
		return
	}
	if err := cc.ComicLogic.ComicLookFor(auth.NewAuthObjectByHttp(ctx), request.Title, request.Link); err != nil {
		response.SetCode(json_response.CODE_ERROR).SetMsg(err.Error()).Send()
		return
	}
	response.SetCode(json_response.CODE_SUCCESS).SetMsg("提交成功").Send()
	return
}
